home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Containrs / sa / array3 < prev    next >
Text File  |  1996-08-01  |  8KB  |  218 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  3. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  4. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  5. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  6. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  7. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  8.  
  9. class ARRAY3{T} < $ELT{T} is
  10.    -- Three-dimensional arrays of elements of type T.
  11.    private include AREF{T}   aclear->aclear, array_ptr->array_ptr;
  12.  
  13.    readonly attr size1:INT;  -- Size of the slowest changing dimension.   
  14.    readonly attr size2:INT;    -- Size of the second fastest changing dimension.
  15.    readonly attr size3:INT;    -- Size of the fastest changing dimension.
  16.    private attr size23:INT;  -- size2*size3, pre-multiplied to spare a 
  17.                         -- multiplication in accessing the array
  18.  
  19.    create(d1,d2,d3:INT):SAME  pre d1>0 and d2>0 and d3>0 is
  20.       -- A new three-dimensional array with dimensions `d1 x d2 x d3'
  21.       -- initialized to void.
  22.       res::=new(d1*d2*d3);
  23.       res.size1:=d1;
  24.       res.size2:=d2;
  25.       res.size3:=d3;
  26.       res.size23:=d2*d3;
  27.       return(res); end;
  28.  
  29.    create(a: ARRAY{ARRAY{ARRAY{T}}}): SAME  
  30.       pre a.size > 0 and a[0].size > 0 and a[0][0].size > 0 is
  31.       -- Create a new array with the same dimensions and values as
  32.       -- a, which is an array of array(arrays(rows)). 
  33.       -- Assume that all the rows of "a" have the same number of elements
  34.       sz1 ::= a.size;
  35.       sz2 ::= a[0].size;
  36.       sz3 ::= a[0][0].size;
  37.       res ::= #SAME(sz1,sz2,sz3);
  38.       loop r::=sz1.times!; 
  39.      loop c::=sz2.times!; 
  40.         loop e::=sz3.times!; res[r,c,e] := a[r][c][e]; end end end;
  41.       return(res);
  42.       end;
  43.      
  44.    copy: SAME pre ~void(self) and nr > 0 and nc > 0 and ne > 0 is
  45.       -- Return a new 3D array with the same set of values as self
  46.       res ::= #SAME(nr,nc,ne);
  47.       res.acopy(self);
  48.       return(res);
  49.       end;
  50.       
  51.    aget(i1,i2,i3:INT):T is
  52.       -- The element with indices `[i1,i2,i3]'.
  53.       return([i1*(size23)+i2*size3+i3]) end;
  54.  
  55.    aset(i1,i2,i3:INT,val:T) is
  56.       -- Set the element with indices `[i1,i2,i3]' to val.
  57.       [i1*(size23)+i2*size3+i3]:=val end;      
  58.    
  59. -- obsolete
  60.    nr: INT is 
  61.       -- The size of the first dimension of the array. Number of rows
  62.       return size1 end;
  63.    
  64. -- obsolete
  65.    nc: INT is
  66.       -- The size of the second dimension of the array. Number of cols
  67.       return size2 end;
  68. -- obsolete
  69.    ne: INT is
  70.       -- The size of the third dimension of the array. Number of elements
  71.       return size3 end;
  72.  
  73.    ind1!: INT is
  74.       -- Yield each value of the first index in order. The rows
  75.       loop yield(size1.times!); end end;
  76.    
  77.    ind2!:INT is
  78.       -- Yield each value of the second index in order. The columns
  79.       loop yield(size2.times!); end end;
  80.  
  81.    ind3!:INT is
  82.       -- Yield each value of the third index in order. The elements
  83.       loop yield(size3.times!); end end;
  84.  
  85.    row_ind!: INT is
  86.       -- Yield each value of the first index in order. The rows
  87.       loop yield(size1.times!); end end;
  88.    
  89.    col_ind!:INT is
  90.       -- Yield each value of the second index in order. The columns
  91.       loop yield(size2.times!); end end;
  92.  
  93.    elem_ind!:INT is
  94.       -- Yield each value of the third index in order. The elements
  95.       loop yield(size3.times!); end end;
  96.    
  97.    diag_elt!: T is
  98.       -- Yield values along the diagonal (square in smaller dimension)
  99.       loop ind ::= (nr.min(nc).min(ne)).times!; yield([ind,ind,ind]) end; end;
  100.  
  101.    set_diag_elt!(val:T) is
  102.       -- Set values along the diagonal (square in smaller dimension)
  103.       loop id ::= (nr.min(nc).min(ne)).times!; [id,id,id] := val; yield;  end; end;
  104.    
  105.    inds!:TUP{INT,INT,INT} is
  106.       -- Yield tuples of the indices of self in lexicographical order.
  107.       loop row ::=size1.times!;
  108.      loop col::=size2.times!;
  109.         loop yield(#TUP{INT,INT,INT}(row,col,size3.times!)); end end end end;
  110.  
  111.    elt!: T is
  112.       -- Yield all elements in row major order
  113.       loop yield(aelt!) end; end;
  114.    
  115.    set!(val:T) is
  116.       -- Set all elements in row major order
  117.       loop aset!(val); yield; end end;
  118. ---------------------
  119.    row_elt!(once row,once elem:INT):T is
  120.       -- Yield elements by varying index 2 and holding index 1 at `row' 
  121.       -- and index 3 at `elem'.
  122.       -- The elements of a row "row"
  123.       --      loop yield(aelt!(row*nc,nc,1)); end end;
  124.       loop yield([row,col_ind!,elem]); end; end;
  125.  
  126.    col_elt!(once col,once elem:INT):T is
  127.       -- Yield elements by varying index 1 and holding index 2 at `col'
  128.       -- and index 3 at `elem'.
  129.       -- The elements of a "column" col
  130.       --      loop yield(aelt!(col,nr,nc)); end end;
  131.       loop yield([row_ind!,col,elem]); end; end;
  132.  
  133.    elem_elt!(once row,once col:INT):T is
  134.       -- Yield elements by varying index 3 and holding index 2 at `col'
  135.       -- and index 1 at `row'.
  136.       -- The elements of a "elements (third dimension)" elem
  137.       loop yield([row,col,elem_ind!]); end; end;
  138.  
  139.    set_row!(once row,once elem:INT, val:T) is
  140.       -- Set to val elements with varying index 2 and index 1 fixed at `row'
  141.       -- and the index 3 fixed at `elem'.
  142.       -- i.e. setting a row "row"
  143.       --      loop aset!(row*nc,nc,val); yield end end;
  144.       loop [row,col_ind!,elem]:=val; yield; end; end;
  145.  
  146.    set_col!(once col,once elem:INT, val:T) is
  147.       -- Set to val elements with varying index 1 and index 2 fixed at `col'
  148.       -- and index 3 fixed at `elem'.
  149.       -- i.e. setting the column col 
  150.       --      loop aset!(col,nr,nc,val); yield end end;
  151.       loop [row_ind!,col,elem]:=val; yield; end; end;
  152.  
  153.    set_elem!(once row,once col:INT, val:T) is
  154.       -- Set to val elements with varying index 1 and index 2 fixed at `col'
  155.       -- and index 3 fixed at `elem'.
  156.       -- i.e. setting the column col 
  157.       --      loop aset!(col,nr,nc,val); yield end end;
  158.       loop [row,col,elem_ind!]:=val; yield; end; end;
  159.  
  160.    elt1!(once col,once elem:INT):T is
  161.       -- Yield elements by varying index 1 and holding index 2 at `col'
  162.       -- and index 3 at `elem'.
  163.       -- The elements of a "column" col
  164.       --      loop yield(aelt!(col,nr,nc)); end end;
  165.       loop yield([ind1!,col,elem]); end; end;
  166.  
  167.    elt2!(once row,once elem:INT):T is
  168.       -- Yield elements by varying index 2 and holding index 1 at `row' 
  169.       -- and index 3 at `elem'.
  170.       -- The elements of a row "row"
  171.       --      loop yield(aelt!(row*nc,nc,1)); end end;
  172.       loop yield([row,ind2!,elem]); end; end;
  173.  
  174.    elt3!(once row,once col:INT):T is
  175.       -- Yield elements by varying index 3 and holding index 2 at `col'
  176.       -- and index 1 at `row'.
  177.       -- The elements of a "elements (third dimension)" elem
  178.       loop yield([row,col,ind3!]); end; end;
  179.  
  180.    set1!(once col,once elem:INT, val:T) is
  181.       -- Set to val elements with varying index 1 and index 2 fixed at `col'
  182.       -- and index 3 fixed at `elem'.
  183.       -- i.e. setting the column col 
  184.       --      loop aset!(col,nr,nc,val); yield end end;
  185.       loop [ind1!,col,elem]:=val; yield; end; end;
  186.  
  187.    set2!(once row,once elem:INT, val:T) is
  188.       -- Set to val elements with varying index 2 and index 1 fixed at `row'
  189.       -- and the index 3 fixed at `elem'.
  190.       -- i.e. setting a row "row"
  191.       --      loop aset!(row*nc,nc,val); yield end end;
  192.       loop [row,ind2!,elem]:=val; yield; end; end;
  193.  
  194.    set3!(once row,once col:INT, val:T) is
  195.       -- Set to val elements with varying index 1 and index 2 fixed at `col'
  196.       -- and index 3 fixed at `elem'.
  197.       -- i.e. setting the column col 
  198.       --      loop aset!(col,nr,nc,val); yield end end;
  199.       loop [row,col,ind3!]:=val; yield; end; end;
  200.  
  201. ----------------------------
  202.       
  203.    transpose: SAME is
  204.       -- Return a new array containing the transpose of self
  205.       res ::= #SAME(nc,nr,ne);
  206.       res.to_transpose_of(self);
  207.       return(res) end;
  208.    
  209.    to_transpose_of(a1:SAME)
  210.       -- Set self to the transpose of a1.
  211.       pre a1.nr=nc and a1.nc=nr and a1.ne=ne is
  212.       loop t::=inds!; [t.t1,t.t2,t.t3]:=a1[t.t3,t.t2,t.t1] end;
  213.       end;
  214.  
  215. end; -- class ARRAY3{T}
  216.  
  217. -------------------------------------------------------------------
  218.